package com.library_manage_system.strategy;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.library_manage_system.view.WindowsMainImpl;
import com.library_manager_system.Book;
import com.library_manager_system.utils.GetBookByName;
import com.library_manager_system.utils.LibraryDao;

public class ReturnBookPanelStrategy implements PanelStrategy{

	@Override
	public JPanel createPanel(final ArrayList<Book> list,final WindowsMainImpl windowsMainImpl) {
		// TODO Auto-generated method stub
	
		JPanel returnBookPanel = new JPanel();
		returnBookPanel.setLayout(new FlowLayout());
		// 设置一个label来提示客户信息，提问想要还哪一本书
		JLabel jLabel = new JLabel("请问你想要还哪一本书");

		// 设置一个下拉框用来选择客户想要还哪一本书
		final ArrayList<Book> arraylist = LibraryDao.readBook(2);
		int size = arraylist.size();
		String[] s = new String[size];
		Iterator<Book> it = arraylist.iterator();
		while(it.hasNext()){
			s[size-1] = it.next().getBook_name();
			size--;
		}
		final JComboBox jComboBox = new JComboBox(s);
		JScrollPane jScrollPane = new JScrollPane(jComboBox);
		// 设置一个按钮表示提交想要借的书
		JButton jButton = new JButton("确定");
		jButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//找到要归还的那本书
				String return_book_name = (String)jComboBox.getSelectedItem();
				Book return_book = GetBookByName.getBookByName(arraylist, return_book_name);
				//进行归还
				if(return_book == null){
					JOptionPane.showMessageDialog(null, "这本书已经全部归还了，请点其他界面刷新一下！", 
							"图书馆管理系统",JOptionPane.ERROR_MESSAGE);
					return;
				}
				return_book.huan();
				System.out.println(return_book.getBorrow_book_number());
				//如果借出的本数为0,说明没有借出去了，应把return_book在Statics_borrow_book.txt的记录删除掉,
				//否则，就更新一下Statics_borrow_book.txt
				if(return_book.getBorrow_book_number() == 0){
					System.out.println("借出去的为0");
					arraylist.remove(return_book);
				}
				LibraryDao.updateLibraryDao(arraylist, 2);
				//如果现余本数等于1，说明之前在Library.txt中是没有他的数据的，经过归还他变成了1，所以要在Library.txt中
				//重新保存,否则，找到归还书的这个对象，然后直接更新Library.txt就可以了
				if(return_book.getThe_rest_number() == 1){
					list.add(return_book);
					LibraryDao.updateLibraryDao(list, 1);
				}else{
					Iterator<Book> it1 = list.iterator();
					while(it1.hasNext()){
						Book book = it1.next();
						if(book.getBook_name().equals(return_book.getBook_name())){
							book.huan();
							LibraryDao.updateLibraryDao(list, 1);
							break;
						}
					}
				}

				JOptionPane.showMessageDialog(null, "归还成功");
				WindowsMainImpl.init(list,windowsMainImpl);
			}
		});

		JPanel jPanel = new JPanel();
		jPanel.setLayout(new GridLayout(3, 1, 50, 50));
		jPanel.add(jLabel);
		jPanel.add(jScrollPane);
		jPanel.add(jButton);

		returnBookPanel.add(jPanel);

		return returnBookPanel;

	}

}
